﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MacViet.DataBaseAPI;

public partial class USProducts : System.Web.UI.UserControl
{
    PRODUCT_TYPESDataAPI product_type = new PRODUCT_TYPESDataAPI();
    PRODUCTSDataAPI product = new PRODUCTSDataAPI();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Multiview1.ActiveViewIndex = 0;
            fillProductTypeInGrid1();
            fillProductInGrid2();
        }

    }
    //void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    ////{

    //    int index = Int32.Parse(e.Item.Value);

    //    Multiview1.ActiveViewIndex = index;

    //}
    
    protected void btnProduct_type_Click(object sender, EventArgs e)
    {
        Multiview1.ActiveViewIndex = 0;
    }
    protected void btnProduct_Click(object sender, EventArgs e)
    {
        Multiview1.ActiveViewIndex = 1;
    }
    //Xu ly tren Gridview1:
    protected void GrvProduct_type_SelectedIndexChanged(object sender, EventArgs e)
    {
        //    PRODUCT_TYPESDataAPI product_type = new PRODUCT_TYPESDataAPI();
        //    DataTable dttable = product_type.getAll();
        //    GridView1.DataSource = dttable;
        //    GridView1.DataBind();
    }
    private void fillProductTypeInGrid1()
    {
        int accId = Convert.ToInt32(Session["ACC_ID"]);
        DataTable dttable = product_type.getByACC_ID(accId);
        GrvProduct_type.DataSource = dttable;
        GrvProduct_type.DataBind();
    }
    protected void GrvProduct_type_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GrvProduct_type.EditIndex = e.NewEditIndex;
        fillProductTypeInGrid1();

    }

    protected void GrvProduct_type_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GrvProduct_type.EditIndex = -1;
        fillProductTypeInGrid1();
    }
    protected void GrvProduct_RowDataBound3(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int accId = Convert.ToInt32(Session["ACC_ID"]);
            DropDownList cmbProducts = (DropDownList)e.Row.FindControl("cmdProductType_ID");
            DataTable dttable = product_type.getByACC_ID(accId);
            if (cmbProducts != null)
            {
                cmbProducts.DataSource = dttable;
                cmbProducts.DataBind();
                cmbProducts.SelectedValue = GrvProduct.DataKeys[0].Value.ToString();

            }
        }
    }
    protected void btnDeleteProTyp_Click(object sender, EventArgs e)
    {

        try
        {
            for (int i = 0; i < GrvProduct_type.Rows.Count; i++)
            {
                string id = string.Empty;
                GridViewRow rows = GrvProduct_type.Rows[i];
                bool chekBox = (rows.FindControl("chkptSelect") as CheckBox).Checked;
                if (chekBox)
                {
                    if (id == string.Empty)
                    {
                        id = GrvProduct_type.DataKeys[i].Value.ToString();
                        product_type.deleteByPrimaryKeys(int.Parse(id));
                        fillProductTypeInGrid1();
                    }
                    else
                        id += GrvProduct_type.DataKeys[i].Value.ToString() + ",";
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not DeletedProduct')</script>");
        }

        //catch(Exception ex) 
        //{
        //    Response.Write("<script>alert('Ma con ton tai trong Product ')</script>");
        //}

    }
    //xu ly su kien tren add tren gridview Product. 
    protected void btnAdd_ProTyp_Click(object sender, EventArgs e)
    {
        try
        {


            int accId = Convert.ToInt32(Session["ACC_ID"]);
            TextBox txtNameProduct = (TextBox)GrvProduct_type.FooterRow.FindControl("txtNewProductType_Name");
            product_type.insertPRODUCT_TYPES(accId, txtNameProduct.Text);
            fillProductTypeInGrid1();

        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not Update Product_type')</script>");
        }
    }
    //xu ly cap nhat du lieu tren Gridview Product_Type
    //protected void btnEditProTyp_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        foreach (GridViewRow myRow in GrvProduct_type.Rows)
    //        {
            
    //            TextBox txtName = (TextBox)myRow.FindControl("txtProductType_Name");
    //            int product_Type_ID = Int32.Parse(GrvProduct_type.DataKeys[myRow.RowIndex].Value.ToString());
    //            product_type.updatePRODUCT_TYPES(product_Type_ID, txtName.Text);
    //            GrvProduct_type.EditIndex = -1;
    //            fillProductTypeInGrid1();
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        Response.Write("<script>alert('Error input Data,you can not UpdateProduct_Type')</script>");
    //    }
    //}
   

    protected void GrvProduct_type_RowCommand2(object sender, GridViewCommandEventArgs e)
    {
        //try
        //{
        //    if (e.CommandName.Equals("AddNewProduct"))
        //    {
        //        int accId = Convert.ToInt32(Session["ACC_ID"]);
        //        TextBox txtNameProduct = (TextBox)GrvProduct_type.FooterRow.FindControl("txtNewProductType_Name");
        //        product_type.insertPRODUCT_TYPES(accId, txtNameProduct.Text);
        //        fillProductTypeInGrid1();
        //    }
        //}
        //catch (Exception ex)
        //{
        //    Response.Write("<script>alert('Error input Data,you can not UpdateProduct_type')</script>");
        //}
    }
    protected void GrvProduct_type_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.className='hightlighrow'");
            e.Row.Attributes.Add("onmouseout", "this.className=''");
        }
    }
    //protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    //{
    //    if (e.CommandName.Equals("AddNew"))
    //    {

    //        TextBox txtNewName = (TextBox)GridView1.FooterRow.FindControl("txtNewName");

    //        TextBox txtNewCity = (TextBox)GridView1.FooterRow.FindControl("txtNewCity");


    //        customer.Insert(txtNewName.Text, cmbNewGender.SelectedValue, txtNewCity.Text, txtNewState.Text, cmbNewType.SelectedValue);
    //        FillCustomerInGrid();
    //    }
    //} 
    //xu ly tren Gridview2

    protected void GrvProduct_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GrvProduct.EditIndex = e.NewEditIndex;
        fillProductInGrid2();

    }
    protected void GrvProduct_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GrvProduct.EditIndex = -1;
        fillProductInGrid2();
    }
    private void fillProductInGrid2()
    {
        int accId = Convert.ToInt32(Session["ACC_ID"]);
        DataTable dttable = product.get_Products_Edit(accId);
        GrvProduct.DataSource = dttable;
        GrvProduct.DataBind();
    }


    private DataTable loadProductData()
    {
        int accId = Convert.ToInt32(Session["ACC_ID"]);
        DataTable dt = product_type.getByACC_ID(accId);//get_Products_Edit();
        if (dt != null)
            return dt;
        return null;
    }
    protected void btnDeletePro_Click1(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < GrvProduct.Rows.Count; i++)
            {
                string id = string.Empty;
                GridViewRow rows = GrvProduct.Rows[i];
                bool chekBox = (rows.FindControl("chkpSelect") as CheckBox).Checked;
                if (chekBox)
                {
                    if (id == string.Empty)
                    {
                        id = GrvProduct.DataKeys[i].Value.ToString();
                         product.deleteByPrimaryKeys(int.Parse(id));
                        fillProductInGrid2();
                    }
                    else
                        id += GrvProduct.DataKeys[i].Value.ToString() + ",";
                }
            }
            Response.Write("<script>alert(' Deleted Product success')</script>");
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not DeletedProduct')</script>");
        }

    }


    protected void GrvProduct_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList cmbProducts = (DropDownList)e.Row.FindControl("cmbProductType_ID");

            if (cmbProducts != null)
            {
                cmbProducts.DataSource = loadProductData();
                cmbProducts.DataBind();
                cmbProducts.SelectedValue = GrvProduct.DataKeys[0].Value.ToString();

            }
        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            DropDownList cmbNewProducts = (DropDownList)e.Row.FindControl("cmbNewProductType_ID");
            cmbNewProducts.DataSource = loadProductData();
            cmbNewProducts.DataBind();
        }
    }

    protected void GrvProduct_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            DropDownList cmbProduct = GrvProduct.Rows[e.RowIndex].FindControl("cmbProductType_ID") as DropDownList;
            TextBox txtName = (TextBox)GrvProduct.Rows[e.RowIndex].FindControl("txtProduct_Name");
            TextBox txtDescription = (TextBox)GrvProduct.Rows[e.RowIndex].FindControl("txtProduct_Des");
            TextBox txtPrice = (TextBox)GrvProduct.Rows[e.RowIndex].FindControl("txtProduct_Price");
            TextBox txtQuantity = (TextBox)GrvProduct.Rows[e.RowIndex].FindControl("txtProduct_Quantity");
            int value = Int32.Parse(cmbProduct.SelectedValue.ToString());
            int productID = Int32.Parse(GrvProduct.DataKeys[e.RowIndex].Values[0].ToString());
            int Quantity = Int32.Parse(txtQuantity.Text);
            decimal prices = Decimal.Parse(txtPrice.Text);
            product.updatePRODUCTS(productID, value, txtName.Text, txtDescription.Text, prices, Quantity);
            GrvProduct.EditIndex = -1;
            fillProductInGrid2();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not UpdateProduct')</script>");

        }
    }


    

    protected void btnAdd_Pro_Click(object sender, EventArgs e)
    {
        try
        {
           
            
                DropDownList cmdNewproduct = (DropDownList)GrvProduct.FooterRow.FindControl("cmbNewProductType_ID");
                TextBox txtNewName = (TextBox)GrvProduct.FooterRow.FindControl("txtNewProduct_Name");
                TextBox txtNewDes = (TextBox)GrvProduct.FooterRow.FindControl("txtNewProduct_Des");
                TextBox txtNewPric = (TextBox)GrvProduct.FooterRow.FindControl("txtNewProduct_Price");
                TextBox txtNewQuan = (TextBox)GrvProduct.FooterRow.FindControl("txtNewProduct_Quantity");
                int cmdNew = Int32.Parse(cmdNewproduct.SelectedValue.ToString());
                int accId = Convert.ToInt32(Session["ACC_ID"]);
                decimal prices = Decimal.Parse(txtNewPric.Text);
                int NewQuan = Int32.Parse(txtNewQuan.Text);
                product.insertPRODUCTS(cmdNew, accId, txtNewName.Text, txtNewDes.Text, prices, NewQuan);
                fillProductInGrid2();
            
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not AddProduct')</script>");
        }
    }


    protected void GrvProduct_type_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            TextBox txtName = (TextBox)GrvProduct_type.Rows[e.RowIndex].FindControl("txtProductType_Name");
            int product_Type_ID = Int32.Parse(GrvProduct_type.DataKeys[e.RowIndex].Values[0].ToString());
            product_type.updatePRODUCT_TYPES(product_Type_ID, txtName.Text);
            GrvProduct_type.EditIndex = -1;
            fillProductTypeInGrid1();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not Update Product_Type')</script>");
        }
    }

   
    protected void btnEditProTyp_Click(object sender, EventArgs e)
    {
        //try
        //{
        //    TextBox txtName = (TextBox)GrvProduct_type.Rows.FindControl("txtProductType_Name");
        //    int product_Type_ID = Int32.Parse(GrvProduct_type.DataKeys[e.RowIndex].Values[0].ToString());
        //    product_type.updatePRODUCT_TYPES(product_Type_ID, txtName.Text);
        //    GrvProduct_type.EditIndex = -1;
        //    fillProductTypeInGrid1();
        //}
        //catch (Exception ex)
        //{
        //    Response.Write("<script>alert('Error input Data,you can not Update Product_Type')</script>");
        //}
    }


   
}

